package com.geeklan.smart.ui.pet;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.geeklan.base.base.BaseActivity;
import com.geeklan.smart.R;
import com.geeklan.smart.widget.ItemOnClickListener;
import com.geeklan.smart.databinding.ActivitySelectPetBinding;
import com.geeklan.smart.ui.pet.adapter.PetAdapter;
import com.geeklan.smart.utils.AssetsImagePathReader;
import com.geeklan.smart.utils.FileUtils;
import com.geeklan.smart.utils.ThreadTask;

import java.util.ArrayList;
import java.util.List;

public class SelectPetActivity extends BaseActivity<ActivitySelectPetBinding> implements View.OnClickListener , ItemOnClickListener {
    private List<String> catImagePaths;
    private List<String> dogImagePaths;
    private List<String> imagePaths = new ArrayList<>();
    private ArrayList<String> photoPaths = new ArrayList<>();
    private PetAdapter petAdapter;
    private int petType = 1;
    private String headUri;
    private String etPetName;
    private boolean isEdit = false;
    @Override
    protected ActivitySelectPetBinding initBinding() {
        return ActivitySelectPetBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initData() {
        isEdit = getIntent().getBooleanExtra("isEdit",false);
        if(!isEdit){
            photoPaths = getIntent().getStringArrayListExtra("photoPaths");

            headUri = getIntent().getStringExtra("headUri");
            etPetName = getIntent().getStringExtra("petName");
        }
        loading();
        ThreadTask.executeWithCallback(() -> {
            catImagePaths = AssetsImagePathReader.getImagePathsFromAssets(this, "cat");
            dogImagePaths = AssetsImagePathReader.getImagePathsFromAssets(this, "dog");
        }, new ThreadTask.Callback() {
            @Override
            public void onSuccess() { handler.sendEmptyMessage(0); }
            @Override
            public void onError(Exception e) {
                handler.sendEmptyMessage(0);
            }
        });
    }

    Handler handler = new Handler(msg -> {
        dismiss();
        imagePaths.clear();
        imagePaths.addAll(catImagePaths);
        petAdapter.setData(imagePaths);
        return false;
    });

    @Override
    protected void initView() {
        mBinding.ivBack.setOnClickListener(v -> finish());
        petAdapter = new PetAdapter(this);
        petAdapter.setItemOnClickListener(this);
        mBinding.rvPet.setLayoutManager(new LinearLayoutManager(this));
        mBinding.rvPet.setAdapter(petAdapter);
        mBinding.viewCat.setOnClickListener(this);
        mBinding.viewDog.setOnClickListener(this);
        mBinding.ivBack.setOnClickListener(v -> finish());

        mBinding.etDeviceName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                petAdapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == mBinding.viewCat.getId()){
            petType = 1;
            setViewBackground(0);
            imagePaths.clear();
            imagePaths.addAll(catImagePaths);
            petAdapter.setData(imagePaths);
        } else if(id == mBinding.viewDog.getId()){
            petType = 2;
            setViewBackground(1);
            imagePaths.clear();
            imagePaths.addAll(dogImagePaths);
            petAdapter.setData(imagePaths);
        }
    }

    private void setViewBackground(int index){
        if(index == 0){
            mBinding.viewCat.setBackground(ActivityCompat.getDrawable(this,R.drawable.layout_9c_radius_bg));
            mBinding.viewDog.setBackground(ActivityCompat.getDrawable(this,R.drawable.layout_f5_radius_bg));
        }else {
            mBinding.viewCat.setBackground(ActivityCompat.getDrawable(this,R.drawable.layout_f5_radius_bg));
            mBinding.viewDog.setBackground(ActivityCompat.getDrawable(this,R.drawable.layout_9c_radius_bg));
        }
    }

    @Override
    protected void onApplyWindowInsets() {
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            getWindow().setStatusBarColor(Color.WHITE);
            return insets;
        });
    }

    @Override
    public void networkStatus(boolean isConnected) {

    }

    @Override
    public void networkType(int type) {

    }

    public static void startActivity(Context context, ArrayList<String> pathList, String headUri,String etPetName){
        Intent intent = new Intent(context, SelectPetActivity.class);
        intent.putStringArrayListExtra("photoPaths",pathList);
        intent.putExtra("headUri",headUri.toString());
        intent.putExtra("petName",etPetName);
        intent.putExtra("isEdit",false);
        context.startActivity(intent);
    }

    public static void startActivity(Activity context, boolean isEdit){
        Intent intent = new Intent(context, SelectPetActivity.class);
        intent.putExtra("isEdit",isEdit);
        context.startActivityForResult(intent,100);
    }

    @Override
    public void OnItemClick(View view, int position) {
        String imagePath = imagePaths.get(position);
        String typeName = FileUtils.extractFileNameWithoutExtension(imagePath);
        if(!isEdit) {
            AddPetInfoActivity.startActivity(this, photoPaths, headUri, etPetName, petType, typeName);
        }else {
            Intent intent = new Intent();
            intent.putExtra("typeName", typeName);
            intent.putExtra("petType", petType);
            setResult(RESULT_OK, intent);
            finish(); // 结束当前 Activity
        }
    }
}